package hihocode;

import java.util.Scanner;

public class Offer18Problem2 {

	private static final String FRUIT = "Fruit";
	private static final String ANIMAL = "Animal";
	private static final String RING = "Ring";
	
	private static final int INIT = 10000000;
	
	private class Status{
		int fruitCount;
		int balanaCount;
		int berryCount;
		int cherryCount;
		int lemonCount;
		boolean pig;
		boolean monkey;
		boolean elephant;
		int allCard;	//记录所有的牌
		
		public void reset(){
			fruitCount = 0;
			balanaCount = 0;
			berryCount = 0;
			cherryCount = 0;
			lemonCount = 0;
			pig = false;
			monkey = false;
			elephant = false;
		}
		@Override
		public String toString() {
			return "balana: " + balanaCount + "lemonCount:" + lemonCount + " hasPig " + pig;
		}
	}
	
	public static void main(String[] args) {
		Offer18Problem2 object = new Offer18Problem2();
		object.getRes();
	}
	
	public void getRes(){
		Scanner scanner = new Scanner(System.in);
		while(scanner.hasNext()){
			int players = scanner.nextInt();
			int allCard = 0;
			Status[] playStatus = new Status[players];
			for(int i = 0; i < players; i++){
				playStatus[i] = new Status();
			}
			int[] cards = new int[players];
			int curPlayers = 0;
			int operations = scanner.nextInt();
			scanner.nextLine();
			while(operations-- > 0){
				String input = scanner.nextLine();
				String[] arguments = input.split(" ");
				System.out.println("curOperations: " + arguments[0]);
				if(arguments[0].equals(FRUIT)){
					playStatus[curPlayers].reset();
					playStatus[curPlayers].fruitCount += Integer.parseInt(arguments[1]);
					cards[curPlayers]--;
					allCard++;
					for(int i = 2; i < arguments.length; i++){
						if(arguments[i].equals("0")){
							playStatus[curPlayers].balanaCount++;
						}else if(arguments[i].equals("1")){
							playStatus[curPlayers].berryCount++;
						}else if(arguments[i].equals("2")){
							playStatus[curPlayers].cherryCount++;
						}else if(arguments[i].equals("3")){
							playStatus[curPlayers].lemonCount++;
						}
					}
					curPlayers = (curPlayers + 1) % players;
					System.out.println(playStatus[curPlayers]);
				} else if (arguments[0].equals(ANIMAL)) {
					playStatus[curPlayers].reset();
					cards[curPlayers]--;
					allCard++;
					if (arguments[1].equals("0")) {
						playStatus[curPlayers].monkey = true;
					} else if (arguments[1].equals("1")) {
						playStatus[curPlayers].elephant = true;
					} else if (arguments[1].equals("2")) {
						playStatus[curPlayers].pig = true;
					} 
					System.out.println("ANIIMAL: " + playStatus[curPlayers]);
					curPlayers = (curPlayers + 1) % players;
				} else if (arguments[0].equals(RING)) {
					//如果响铃成功
					//判断谁在响铃
					int cur = Integer.parseInt(arguments[1]);
					Status status = new Status();
					System.out.println(playStatus[0].pig);
					for(int i = 0; i < players; i++){
						status.balanaCount += playStatus[i].balanaCount;
						status.cherryCount += playStatus[i].cherryCount;
						status.berryCount += playStatus[i].berryCount;
						status.lemonCount += playStatus[i].lemonCount;
						status.fruitCount += playStatus[i].fruitCount;
						status.monkey = status.monkey || playStatus[i].monkey;
						status.elephant = status.elephant || playStatus[i].elephant;
						status.pig = status.pig || playStatus[i].pig;
					}
					System.out.println(status.pig + " count: " + status.fruitCount);
					if( (status.balanaCount == 5 || status.cherryCount == 5 || status.berryCount == 5 || 
						status.lemonCount == 5) || (status.fruitCount >= 1 && status.lemonCount == 0 &&
						status.monkey) || (status.fruitCount >= 1 && status.berryCount == 0 &&
						status.elephant) || (status.fruitCount >= 1 && status.pig)){
						System.out.println("Ring " + cur + " cards: " + allCard);
						cards[cur] += allCard;
						allCard = 0;
						for(int i = 0; i < players; i++){
							playStatus[i].reset();
						}
					}else{
						for(int i = 0; i < players; i++){
							cards[i] += 1;
						}
						System.out.println("Ring fail "  + " cards: " + allCard);
						cards[cur] -= players;
					}
					curPlayers = cur;
				}
			}
			for(int i = 0; i < players; i++){
				System.out.println(cards[i] + INIT);
			}
		}
		scanner.close();
	}
}
